package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.vo.*;
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 报表打印Controller
 *
 * @author admin
 * @date 2022-05-03
 */
@RestController
@RequestMapping("/system/jasper")
public class RecordJasperController {
    /**
     * 封皮打印
     */
    @Log(title = "封皮打印", businessType = BusinessType.EXPORT)
    @PostMapping(value = "/cover")
    public void cover(HttpServletResponse response, CoverData coverData) throws IOException {
        //1.引入jasper文件
        Resource resource = new ClassPathResource("jaspers/cover.jasper");
        FileInputStream fis = new FileInputStream(resource.getFile());
        //2.创建JasperPrint,向jasper文件中填充数据
        ServletOutputStream os = response.getOutputStream();
        try {
            Map parameters = new HashMap<>();
            parameters.put("archiveClassifiNum", coverData.getArchiveClassifiNum());
            parameters.put("archiveCode", coverData.getArchiveCode());
            parameters.put("archiveFileNum", coverData.getArchiveFileNum());
            parameters.put("miniatureNum", coverData.getMiniatureNum());
            parameters.put("projectCode", coverData.getProjectCode());
            parameters.put("caseFileTitle", coverData.getCaseFileTitle());
            parameters.put("personLiable", coverData.getPersonLiable());
            parameters.put("compilationDate", coverData.getCompilationDate());
            parameters.put("objStr2", coverData.getObjStr2());
            parameters.put("secretLevel", coverData.getSecretLevel());
            parameters.put("storageTime", coverData.getStorageTime());
            parameters.put("totalRolls", coverData.getTotalRolls());
            parameters.put("currentRolls", coverData.getCurrentRolls());
            parameters.put("registNum", coverData.getRegistNum());
            JasperPrint print = JasperFillManager.fillReport(fis, parameters, new JREmptyDataSource());
            JasperExportManager.exportReportToPdfStream(print, os);
        } catch (JRException e) {
            e.printStackTrace();
        } finally {
            os.flush();
        }
    }

    /**
     * 背脊打印
     */
    @Log(title = "背脊打印", businessType = BusinessType.EXPORT)
    @PostMapping(value = "/back")
    public void back(HttpServletResponse response, @RequestBody BackData backdata) throws IOException {
        // 引入jasper 文件
        Resource resource = new ClassPathResource("jaspers/back.jasper");
        FileInputStream fis = new FileInputStream(resource.getFile());
        //2、创建JasperPrint, 向jasper文件填充数据
        ServletOutputStream os = response.getOutputStream();
        try {
            Map parameters = new HashMap<>();
            BackData bean = new BackData();
            List<BackTableData> list = backdata.getDataList();

            JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(list);
            JasperPrint print = JasperFillManager.fillReport(fis, parameters, ds);
            JasperExportManager.exportReportToPdfStream(print, os);
        } catch (JRException e) {
            e.printStackTrace();
        } finally {
            os.flush();
        }
    }

    /**
     * 卷 内 文 件 目 录 打印
     */
    @Log(title = "卷内文件目录打印", businessType = BusinessType.EXPORT)
    @PostMapping(value = "/menu")
    public void menu(HttpServletResponse response, @RequestBody MenuData menudata) throws IOException {
        // 引入jasper 文件
        Resource resource = new ClassPathResource("jaspers/menu.jasper");
        FileInputStream fis = new FileInputStream(resource.getFile());
        //2、创建JasperPrint, 向jasper文件填充数据
        ServletOutputStream os = response.getOutputStream();
        try {
            Map parameters = new HashMap<>();
            parameters.put("registNum", menudata.getRegistNum());

            List<MenuTableData> list = menudata.getDataList();

            JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(list);
            JasperPrint print = JasperFillManager.fillReport(fis, parameters, ds);
            JasperExportManager.exportReportToPdfStream(print, os);
        } catch (JRException e) {
            e.printStackTrace();
        } finally {
            os.flush();
        }
    }


    /**
     * 卷内备考表打印
     */
    @Log(title = "卷内备考表打印", businessType = BusinessType.EXPORT)
    @PostMapping(value = "/bkTable")
    public void bkTable(HttpServletResponse response, BkTableData bkTableData) throws IOException {
        //1.引入jasper文件
        Resource resource = new ClassPathResource("jaspers/bkTable.jasper");
        FileInputStream fis = new FileInputStream(resource.getFile());
        //2.创建JasperPrint,向jasper文件中填充数据
        ServletOutputStream os = response.getOutputStream();
        try {
            Map parameters = new HashMap<>();
            parameters.put("pageCounts", bkTableData.getPageCounts());
            parameters.put("createBy", bkTableData.getCreateBy());
            parameters.put("updateBy", bkTableData.getUpdateBy());
            //将时间拆分成年月日
            if (!bkTableData.getCreateTime().isEmpty()) {
                String[] s1 = bkTableData.getCreateTime().split(" ");
                String[] t1 = s1[0].split("-");
                parameters.put("createYear", t1[0]);
                parameters.put("createMonth", t1[1]);
                parameters.put("createDay", t1[2]);
            }
            if (!bkTableData.getUpdateTime().isEmpty()) {
                String[] s2 = bkTableData.getUpdateTime().split("-");
                String[] t2 = s2[0].split("-");
                parameters.put("updateYear", t2[0]);
                parameters.put("updateMonth", t2[1]);
                parameters.put("updateDay", t2[2]);
            }

            JasperPrint print = JasperFillManager.fillReport(fis, parameters, new JREmptyDataSource());
            JasperExportManager.exportReportToPdfStream(print, os);
        } catch (JRException e) {
            e.printStackTrace();
        } finally {
            os.flush();
        }
    }

    /**
     * 城建档案移交验收清单
     */
    @Log(title = "城建档案移交验收清单", businessType = BusinessType.EXPORT)
    @PostMapping(value = "/acceptance")
    public void acceptance(HttpServletResponse response, AcceptanceData acceptanceData) throws IOException {
        //1.引入jasper文件
        Resource resource = new ClassPathResource("jaspers/acceptance.jasper");
        FileInputStream fis = new FileInputStream(resource.getFile());
        //2.创建JasperPrint,向jasper文件中填充数据
        ServletOutputStream os = response.getOutputStream();
        try {
            Map parameters = new HashMap<>();
            parameters.put("enginName", acceptanceData.getEnginName());
            parameters.put("unitProjectName", acceptanceData.getUnitProjectName());
            parameters.put("handoverUnit", acceptanceData.getHandoverUnit());
            parameters.put("registNum", acceptanceData.getRegistNum());

            JasperPrint print = JasperFillManager.fillReport(fis, parameters, new JREmptyDataSource());
            JasperExportManager.exportReportToPdfStream(print, os);
        } catch (JRException e) {
            e.printStackTrace();
        } finally {
            os.flush();
        }
    }
}
